import scrapy
from ..items import MoviesItem


class MaoyanSpider(scrapy.Spider):
    name = 'maoyan'
    allowed_domains = ['maoyan.com']
    start_urls = ['https://maoyan.com/films?showType=3']

    def parse(self, response):
        names = response.xpath("//div[@class='channel-detail movie-item-title']/a/text()").extract()
        scores = [score.xpath('string(.)').extract_first() for score in response.xpath("//div[@class='channel-detail channel-detail-orange']")]
        # div_scores = response.xpath("//div[@class='channel-detail channel-detail-orange']")
        # scores = []
        # for score in div_scores:
        #     scores.append(score.xpath('string(.)').extract_first())

        # 返回为字典(dicr)
        for name, score in zip(names, scores):
            # 推送到pipelines.py
            yield {"name": name, "score": score}

        # 返回为Item对象
        # for name, score in zip(names, scores):
        #     item = MoviesItem()
        #     item["name"] = name
        #     item["score"] = score
            # 推送到pipelines.py
            # yield item
